﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace TimeTracker.XmlProvider
{
    public partial class XProjectElement : IProject
    {
        public XElement BaseElement
        {
            get;
            private set;
        }

        public XProjectElement(XElement element)
        {
            this.BaseElement = element;
        }

        public XProjectElement()
        {
            this.BaseElement = new XElement("project");
        }

        IProject IProject.Project
        {
            get
            {
                XElement element = this.BaseElement.Parent.Parent;

                if (element.Name == "project")
                {
                    return new XProjectElement(element);
                }

                return null;
            }
        }

        string IProject.Name
        {
            get
            {
                return this.BaseElement.Attribute("name").Value;
            }
            set
            {
                this.BaseElement.SetAttributeValue("name", value);
            }
        }

        IEnumerable<ISession> IProject.Sessions
        {
            get {
                if (this.BaseElement.Element("sessions") != null)
                {
                    return this.BaseElement.Element("sessions").Elements("session").Select(r => new XSessionElement(r));
                }

                return new ISession[] { };
            }
        }

        IEnumerable<IProject> IProject.Projects
        {
            get
            {
                return this.BaseElement.Elements("project").Cast<XProjectElement>();
            }
        }

        int IComparable.CompareTo(object obj)
        {
            throw new NotImplementedException();
        }
    }
}
